package com.xiaoshuidi.cloud.module.iot.controller.admin.face;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.iot.controller.admin.face.vo.*;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.service.FaceDatabaseService;
import com.xiaoshuidi.cloud.module.iot.task.FaceDatabaseTask;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 人脸库相关接口
 *
 * @Author Feng
 * @date 2023/6/25
 */
@Validated
@RestController
@Tag(name = "智能设备 - 智能门禁")
@RequestMapping(ApiConstants.NAME + "/face-database")
@RequiredArgsConstructor
public class FaceDatabaseController {

    private final FaceDatabaseService faceDatabaseService;
    private final FaceDatabaseTask faceDatabaseTask;

    /**
     * 人脸库 - 列表
     *
     * @param req
     * @return {@link PageResult}<{@link FaceDatabasePageResp}>
     */
    @Operation(summary = "人脸库 - 列表")
    @Parameter(name = "req", description = "人脸库实体类")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:query')")
    public CommonResult<PageResult<FaceDatabasePageResp>> page(FaceDatabasePageReq req) {
        return CommonResult.success(faceDatabaseService.page(req));
    }

    /**
     * 添加人脸信息
     *
     * @param req
     * @return
     */
    @Operation(summary = "添加人脸信息")
    @Parameter(name = "req", description = "人脸库实体类")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:create')")
    public CommonResult<Boolean> create(@RequestBody @Valid FaceDatabaseCreateReq req) {
        return CommonResult.success(faceDatabaseService.create(req));
    }

    /**
     * 删除人脸信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除人脸信息")
    @Parameter(name = "id", description = "人脸信息id")
    @PostMapping("/remove")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:remove')")
    public CommonResult<Boolean> remove(Long id) {
        return CommonResult.success(faceDatabaseService.remove(id));
    }

    /**
     * 获取人脸信息
     *
     * @param id
     */
    @Operation(summary = "获取人脸信息")
    @Parameter(name = "req", description = "人脸库实体类")
    @GetMapping("/get")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:query')")
    public CommonResult<FaceDatabaseResp> get(@RequestParam("id") @NotNull(message = "请选择一个人脸信息") Long id) {
        return CommonResult.success(faceDatabaseService.get(id));
    }

    /**
     * 审核操作
     *
     * @param req
     * @return
     */
    @Operation(summary = "审核操作")
    @Parameter(name = "req", description = "人脸库实体类")
    @PostMapping("/approve")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:approve')")
    public CommonResult<Boolean> approve(@RequestBody @Valid FaceDatabaseApproveReq req) {
        return CommonResult.success(faceDatabaseService.approve(req));
    }

    /**
     * 根据ID修改
     *
     * @param req
     * @return
     */
    @Operation(summary = "根据ID修改")
    @Parameter(name = "req", description = "人脸库实体类")
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasAnyPermissions('iot:face-database:edit')")
    public CommonResult<Boolean> edit(@RequestBody @Valid FaceDatabaseEditReq req) {
        return CommonResult.success(faceDatabaseService.edit(req));
    }

    /**
     * 测试 - 失效过去三天人脸信息
     */
    @Operation(summary = "失效过去三天人脸信息")
    @Parameter(name = "req", description = "人脸库实体类")
    @GetMapping("/test-invalidity-face")
    public void testInvalidityFace() {
        faceDatabaseTask.invalidityFace();
    }
}
